import { HTTP } from '../../../utils/http.js'
import api from '../../../utils/api.js'
import { config } from '../../../utils/config'
import utils from "../../../utils/util"
let http = new HTTP()
var app = getApp();

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		orderType: false, //选择订单类型
		imgUrl: config.imgUrl,
		orderList: [],
		total: 0,
		pageNum: 1,
		pageSize: 10,
		payBtn: true,
		inputDelete: false,
		inputValue: '',
		type: 0,
		isfind: false, //是否查找过 缺省状态判断

	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		let keyword = options.keyword
		if (keyword) {
			this.setData({
				keyword
			})
			this.getOrderList();

		}
	},

	getOrderList() {
		let that = this;
		wx.showLoading({ title: '加载中' })
		http.request({
			url: api.integralOrderSearch,
			method: 'GET',
			data: {
				keyword: that.data.keyword,
			},
			success: (res) => {
				console.log('getOrderList', res.data)
				if (res.code == 200) {
					let result = res.data
					that.setData({
						total: result.total,
						orderList: that.data.orderList.concat(result.data),
						totalPage: result.last_page,
						isfind: true
					})
				}
				wx.hideLoading();
			},
			fail: (res) => {
				wx.hideLoading();
			},
			complete: () => {

			}
		})
	},
	payOrder(e) {
		if (!this.data.payBtn) {
			return false
		} else {
			this.setData({
				payBtn: false
			})
		}
		let that = this
		let order_no = e.currentTarget.dataset.order;
		return new Promise((resolve, reject) => {
			http.request({
				url: api.integralPay,
				data: {
					order_no
				},
				success: res => {
					if (res.code == 200) {
						let data = res.data
						// 调起微信支付
						wx.requestPayment({
							'timeStamp': data.timeStamp,
							'nonceStr': data.nonceStr,
							'package': data.package,
							'signType': 'MD5',
							'paySign': data.paySign,
							'success': function (res) {
								console.log('支付成功', res);

								let orderList = that.data.orderList;
								orderList.forEach(item => {
									if (item.order_no == order_no) {
										item.status_text = '待发货';
										item.status = 2;
										item.is_cancellable = false;
										item.is_canbedeleted = false;
										item.is_receipt = false
									}
								})
								that.setData({
									orderList
								})


							},
							'fail': function (res) {

								console.log('支付失败', res);
								reject(res)

							},
							'complete': function (res) {
								that.setData({
									payBtn: true
								})
								that.payNotify(order_no)
							}
						})

					}
				},
				fail(res) {

				}
			})
		})
	},
	// 支付接口调用完成之后订单查询
	payNotify(order_no) {
		let that = this;
		console.log('支付回调');

		http.request({
			url: api.integralPayNotify,
			method: "POST",
			data: {
				order_no: order_no
			},
			success: (res) => {
				console.log(res);
				if (res.code == 200) {
					// 支付成功需要修改菜篮

					//支付成功更改缓存payList

				}
			},
			fail: (res) => {

			}
		})

	},

	//根据状态查询订单
	anyOrder(e) {
		let orderType = e.currentTarget.dataset.type;
		let orderItem = this.data.orderItem;
		let typeObj = orderItem.find(item => {
			return item.type == orderType
		})
		wx.setNavigationBarTitle({
			title: typeObj.typeName,
		})
		this.setData({
			type: orderType,
			pageNum: 1,
			orderList: [],
			orderType: false,
			isfind: false
		})
		this.getOrderList();

	},

	// 点击订单详情
	orderDetailHandle(e) {
		let order_no = e.currentTarget.dataset.order;
		let paymeth = e.currentTarget.dataset.paymeth;
		wx.navigateTo({
			url: `/pages/pointsMall/orderDetail/orderDetail?order=${order_no}&paymeth=${paymeth}`
		})
	},
	// 取消订单
	cancelOrder(e) {
		let that = this;
		let order_no = e.currentTarget.dataset.order;
		wx.showModal({
			title: '提示',
			content: '确定取消订单',
			confirmColor: '#EA5D56',
			success(res) {
				if (res.confirm) {

					http.request({
						url: api.integralCancelOrder,
						method: "POST",
						data: {
							order_no
						},
						success: (res) => {
							console.log(res);
							if (res.code == 200) {
								let orderList = that.data.orderList;
								orderList.forEach(item => {
									if (item.order_no == order_no) {
										item.status_text = '已取消';
										item.status = 0;
										item.is_cancellable = false;
										item.is_canbedeleted = true;
									}
								})
								that.setData({
									orderList
								})
							}
						},
						fail: (res) => {

						}
					})

				} else if (res.cancel) {
					return;
				}
			}
		})


	},
	//删除订单
	deleteOrder(e) {
		let that = this;
		let order_no;
		if (e && e.currentTarget.dataset.order) {
			order_no = e.currentTarget.dataset.order;
		} else {
			order_no = this.data.deleteOrder
		}
		if (this.data.deleteOrder) {
			this.delOrderApi(this.data.deleteOrder)
		} else {
			wx.showModal({
				title: '提示',
				content: '确定删除订单',
				confirmColor: '#EA5D56',
				success(res) {
					if (res.confirm) {

						that.delOrderApi(order_no)

					} else if (res.cancel) {
						return;
					}
				}
			})
		}


	},
	/*删除订单api*/
	delOrderApi(order_no) {
		let that = this
		http.request({
			url: api.integralDelOrder,
			method: "POST",
			data: {
				order_no
			},
			success: (res) => {
				console.log(res);
				if (res.code == 200) {
					// 支付成功需要修改菜篮
					// wx.showToast({
					//     title: '删除成功',
					//     icon: 'success',
					//     duration: 1500
					// })

					let orderList = that.data.orderList
					orderList.forEach((item, index) => {
						if (item.order_no == order_no) {
							orderList.splice(index, 1)
						}
					})
					that.setData({
						orderList,
						deleteOrder: ''
					})

				}
			},
			fail: (res) => {

			}
		})
	},


	/*搜索按钮*/

	searchHandle(e) {
		let inputValue = e.detail.inputValue
		this.setData({
			inputValue
		})
		wx.navigateTo({
			url: '/pages/pointsMall/searchList/searchList?keyword=' + inputValue
		})
	},
	/*删除搜索框内容*/

	confirmReceipt(e) {
		let that = this
		let order_no = e.currentTarget.dataset.order;
		wx.showModal({
			title: '提示',
			content: '确定收货',
			confirmColor: '#EA5D56',
			success(res) {
				if (res.confirm) {

					http.request({
						url: api.integralConfirmReceipt,
						method: "POST",
						data: {
							order_no
						},
						success: (res) => {
							console.log(res);
							if (res.code == 200) {

								let orderList = that.data.orderList;
								orderList.forEach(item => {
									if (item.order_no == order_no) {
										item.status_text = '待评***价';
										item.status = 4;
										item.is_cancellable = false;
										item.is_canbedeleted = false;
										item.is_receipt = false
									}
								})
								that.setData({
									orderList
								})

							}
						},
						fail: (res) => {

						}
					})


				} else if (res.cancel) {
					return;
				}
			}
		})
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function (options) {
		let that = this
		// 删除订单
		if (this.data.deleteOrder) {
			this.deleteOrder()
		}
		// 取消的订单
		if (this.data.cancelOrderBack) {
			let orderList = that.data.orderList;
			orderList.forEach(item => {
				if (item.order_no == this.data.cancelOrderBack) {
					item.status_text = '已取消';
					item.status = 0;
					item.is_cancellable = false;
					item.is_canbedeleted = true;
				}
			})
			that.setData({
				orderList
			})
		}
		// 点击订单退款
		if (this.data.refundOrderBack) {
			let orderList = that.data.orderList;
			orderList.forEach(item => {
				if (item.order_no == this.data.refundOrderBack) {
					item.refund = 2;
				}
			})
			that.setData({
				orderList
			})
		}
		// 支付成功
		if (this.data.payOrderBack) {
			let orderList = that.data.orderList;
			orderList.forEach(item => {
				if (item.order_no == this.data.payOrderBack) {
					item.status_text = '已支付';
					item.status = 2;
					item.is_cancellable = false;
					item.is_canbedeleted = false;
					item.is_receipt = false
				}
			})
			that.setData({
				orderList
			})

		}
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	// /**
	//  * 页面相关事件处理函数--监听用户下拉动作
	//  */
	// onPullDownRefresh: function() {
	//     console.log('下拉刷新');
	//     if (this.data.pageNum < this.data.totalPage) {
	//         this.setData({
	//             pageNum: pageNum + 1
	//         })
	//         this.getOrderList();
	//     }
	//     wx.stopPullDownRefresh();

	// },

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {
		console.log('触底刷新');
		if (this.data.pageNum < this.data.totalPage) {
			this.setData({
				pageNum: this.data.pageNum + 1
			})
			this.getOrderList();
		}
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})